package ru.sh.ui;

import ru.sh.screen.SHGameScreen;
import ru.sh.stats.ShotPower;
import android.util.Log;
import framework.GameResources;
import framework.GameScreen;
import framework.managers.TextureManager;
import framework.objects.hero.Hero;
import framework.objects.ui.UI;
import framework.objects.ui.buttons.CircleMultitouchButton;

public class FireBtn extends CircleMultitouchButton implements Runnable {

	private static final int STAGE_WAITING_TIME = 50;
	
	private boolean fireBtnDown = false;
	
	private Hero hero;
	
	public FireBtn(GameScreen screen, UI ui, float x0, float y0) {
		super(screen, ui, x0, y0);
		hero = ((SHGameScreen)screen).getHero();
	}
	
	private Thread preFireThread;

	public void run() {
		
		int pressStage = 0;
		while (fireBtnDown && pressStage < 10) {
			pressStage++;
			ShotPower.setValue(pressStage);			
			waitTime();
		}
		if (pressStage == 10) {
			hero.fire();
			hero.look();
			fireBtnDown = false;
		}
		ShotPower.setValue(0);
	}
	
	private void waitTime() {
		try {
			Thread.sleep(STAGE_WAITING_TIME);
		} catch (InterruptedException e) {
			Log.e("Fire Thread", "Interrupt Fire Thread", e);
		}
	}

	@Override
	protected void onDown() {
		fireBtnDown = true;
		hero.preFire();
		preFireThread = new Thread(this);
		preFireThread.start();
	}

	@Override
	protected void onUp() {
		fireBtnDown = false;
		hero.look();
	}

	@Override
	protected void onOut() {
		fireBtnDown = false;
		hero.look();
	}

	@Override
	protected void initPresentation() {
		presentation = TextureManager.getTexture(GameResources.getBitmapName(3));
	}

	@Override
	protected void onOver() {
		// TODO Auto-generated method stub
		
	}

}
